<?php

class PagamentoController extends AppController {
   

    public function beforeFilter(){
		parent::beforeFilter();
		// tutte le azioni di questo controller possono essere eseguite
		 $this->Auth->allow("view");
    }
    
    
    
    public function index(){
        $this->set("pagamenti",$this->Pagamento->find("all"));
    }
    
    public function edit($id){
	    $this->layout="admin";
        $this->loadModel("Bookmaker");
        $bookmaker_list = $this->Bookmaker->find("all",array("recursive" => -1, "conditions" => array("Bookmaker.deleted" => 0)));
        $linkmodel = $this->Pagamento->read(null,$id);
        // pr($linkmodel);die;
        
        $this->set("pagamento",$linkmodel);
        $this->set("bookmaker_list",$bookmaker_list);
        
    }
    
    //ATTENZIONE: **** gli passo in caso il bookmaker parent se ce l'ho!!****
    public function add($idBookmaker=null){
	    $this->layout="admin";
        $this->loadModel("Bookmaker");        
        $bookmaker_list = $this->Bookmaker->find("all",array("recursive" => -1, "conditions" => array("Bookmaker.deleted" => 0)));
        $this->Bookmaker->recursive = -1;
        $bookmodel = $this->Bookmaker->read(null,$idBookmaker);
        $this->set("bm",$bookmodel);
        $this->set("bookmaker_list",$bookmaker_list);
    }
    
    public function save($id=null){
        $this->Pagamento->set($this->request->data);
       // pr($this->request->data);die;
        $this->Pagamento->save();
        
        $this->Session->setFlash("Modalità Pagamento aggiornata con successo");
        $this->redirect("/Pagamento/edit/".$this->Pagamento->id);
        
    }
    
    public function delete($value)
    {
        $this->Pagamento->read(null,$value);
        $this->Pagamento->delete();
              
        $this->Session->setFlash("Pagamento eliminato con successo!");
        $this->redirect("/Pagamento/index");
    }
	
	public function view($bookmaker,$metodo) {
		$query = "select distinct p.id from bookmaker b inner join pagamento p on b.id = p.bookmaker_id and b.deleted=0 and b.nome_slug = " . StringUtil::stringToSql($bookmaker) . " inner join categories_lang c on p.metodo = c.reference_id and c.name_slug = " . StringUtil::stringToSql($metodo);
		$result = $this->Pagamento->query($query);
		$this->Pagamento->recursive=2;
   	    $this->Pagamento->Metodo->hasMany["CategoryLang"]["conditions"]=array("CategoryLang.locale" => Configure::read("Config.language"));
   	    $this->Pagamento->hasMany["PagamentoLang"]["conditions"]=array("PagamentoLang.locale" => Configure::read("Config.language"));
   	    $this->Pagamento->Bookmaker->hasMany["BookmakerLang"]["conditions"]=array("BookmakerLang.locale" => Configure::read("Config.language"));
        $linkmodel = $this->Pagamento->read(null,$result[0]["p"]["id"]);
		
		// pr($linkmodel);

		$this->set("title_for_layout",  __("%s metodo di pagamento %s", $linkmodel["Bookmaker"]["nome"], $linkmodel["Metodo"]["CategoryLang"]["0"]["name"]));
		$this->set("meta_description",  __("Saperne di più sul metodo di pagamento %s del bookmaker %s", $linkmodel["Metodo"]["CategoryLang"]["0"]["name"], $linkmodel["Bookmaker"]["nome"]));

        $this->set("pagamento",$linkmodel);
	}

}

?>